package ch.christianschweizer.epk.common.framework;

import oracle.adf.share.logging.ADFLogger;

import oracle.jbo.server.SQLBuilder;


public class ViewObjectImpl extends oracle.jbo.server.ViewObjectImpl {

    private static final ADFLogger logger = ADFLogger.createADFLogger(ViewObjectImpl.class);
    private static final String format = "executeQueryForCollection: View=%s, Query=%s, Duration=%s, Parameters=%s";

    protected void executeQueryForCollection(Object rowSet, Object[] parameters, int noUserParams) {
        long start = System.currentTimeMillis();
        super.executeQueryForCollection(rowSet, parameters, noUserParams);
        long end = System.currentTimeMillis();

        long duration = end - start;

        logger.fine(String.format(format, getFullName(), getQuery(), duration, getParameters(parameters)));
    }

    private String getParameters(Object[] parameters) {
        StringBuilder sb = new StringBuilder();
        if (parameters != null && getBindingStyle() == SQLBuilder.BINDING_STYLE_ORACLE_NAME) {
            for (Object parameter : parameters) {
                Object[] value = (Object[])parameter;
                sb.append("[" + value[0] + " = ");
                sb.append(value[1] + "] ");
            }
        }
        return sb.toString().trim();
    }
}
